package edu.uta.cse6339.facetedinterface.ui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JCheckBox;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

import edu.uta.cse6339.facetedinterface.read.article.labels.CrimeLabeledArticles;
import edu.uta.cse6339.facetedinterface.read.article.labels.HateCrimeLabeledArticles;
import edu.uta.cse6339.facetedinterface.read.article.labels.StateLabeledArticles;
import edu.uta.cse6339.facetedinterface.read.article.labels.UniversityLabeledArticles;
import edu.uta.cse6339.facetedinterface.util.StateAggrAssaultRangeValues;
import edu.uta.cse6339.facetedinterface.util.StateFirearmRangeValues;
import edu.uta.cse6339.facetedinterface.util.StateMurderRangeValues;
import edu.uta.cse6339.facetedinterface.util.StateRobberyRangeValues;
import edu.uta.cse6339.facetedinterface.util.StateSexAsltRangeValues;
import edu.uta.cse6339.facetedinterface.util.StateViolentCrimeRangeValues;
import edu.uta.cse6339.facetedinterface.util.UniversityMurderRangeValues;
import edu.uta.cse6339.facetedinterface.util.UniversityRobberyRangeValues;
import edu.uta.cse6339.facetedinterface.util.UniversitySexAsltRangeValues;
import edu.uta.cse6339.facetedinterface.util.UniversityVlntCrimeRangeValues;

public class ApplicationWindow {

	private static final String STATE_MURDER_CLASS="Murder_1";
	private static final String STATE_ROBBERY_CLASS="Robbery_1";
	private static final String STATE_ASSAULT_CLASS="Aggravated_Assault_1";
	private static final String STATE_VLNT_CRIME_CLASS="Violent_Crime_1";
	private static final String STATE_SEXUAL_ASLT_CLASS="Sexual_Assault_1";
	private static final String STATE_FIRE_ARM_CLASS="Households _with_firearm_1";			
	
	private static final String UNIV_MURDER_CLASS="Murder_2";
	private static final String UNIV_ROBBERY_CLASS="Robbery_2";
	private static final String UNIV_VLNT_CRIME_CLASS="Violent_Crime_2";
	private static final String UNIV_SEXUAL_CRIME_CLASS="Sexual_Assault_2";
	
	
	private HashMap<String,Set<String>> facetArtileMap = new HashMap<String, Set<String>>();
	
	private JFrame frmFacetedInterface;
	private JEditorPane editPaneArticles = new JEditorPane();
	

	final JSlider sliderMurder = new JSlider();
	final JSlider sliderRobbery = new JSlider();
	final JSlider sliderSexAslt = new JSlider();
	final JSlider sliderFireArm = new JSlider();
	final JSlider sliderAssault = new JSlider();
	final JSlider sliderVlntCrime = new JSlider();
	
	final JSlider sliderMurderUniv = new JSlider();
	final JSlider sliderRoberyUniv = new JSlider();
	final JSlider sliderSexAsltUniv = new JSlider();
	final JSlider sliderVioCrmUniv = new JSlider();
	
	JCheckBox chckbxSexual = new JCheckBox("Sexual");
	JCheckBox chckbxRace = new JCheckBox("Race");
	
	
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
				StateMurderRangeValues.initialiseRange();
				StateRobberyRangeValues.initialiseRange();
				StateAggrAssaultRangeValues.initialiseRange();
				StateFirearmRangeValues.initialiseRange();
				StateSexAsltRangeValues.initialiseRange();
				StateViolentCrimeRangeValues.initialiseRange();
				UniversityMurderRangeValues.initialiseRange();
				UniversityVlntCrimeRangeValues.initialiseRange();
				UniversityRobberyRangeValues.initialiseRange();
				UniversitySexAsltRangeValues.initialiseRange();
				
				CrimeLabeledArticles.initialise();
				StateLabeledArticles.initialise();
				UniversityLabeledArticles.initialise();
				HateCrimeLabeledArticles.initialise();
				
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (Throwable e) {
			e.printStackTrace();
		}
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ApplicationWindow window = new ApplicationWindow();
					window.frmFacetedInterface.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	
	
	public void enableFacets(){

		Set<String> murderSet =CrimeLabeledArticles.getArticlesByLabel(STATE_MURDER_CLASS);
		if(murderSet==null || murderSet.size()==0){
			sliderMurder.setEnabled(false);
		}
		
		Set<String> robberSet =CrimeLabeledArticles.getArticlesByLabel(STATE_ROBBERY_CLASS);
		if(robberSet==null || robberSet.size()==0){
			sliderRobbery.setEnabled(false);
		}
		
		Set<String> aggAsltSet = CrimeLabeledArticles.getArticlesByLabel(STATE_ASSAULT_CLASS);
		if(aggAsltSet==null || aggAsltSet.size()==0){
			sliderAssault.setEnabled(false);
		}
		
		Set<String> sexAsltSet = CrimeLabeledArticles.getArticlesByLabel(STATE_SEXUAL_ASLT_CLASS);
		if(sexAsltSet==null || sexAsltSet.size()==0){
			sliderSexAslt.setEnabled(false);
		}
		
		Set<String> vlntCrimSet = CrimeLabeledArticles.getArticlesByLabel(STATE_VLNT_CRIME_CLASS);
		if(vlntCrimSet==null || vlntCrimSet.size()==0){
			sliderVlntCrime.setEnabled(false);
		}
		
		Set<String> colMurderSet = CrimeLabeledArticles.getArticlesByLabel(UNIV_MURDER_CLASS);
		if(colMurderSet==null || colMurderSet.size()==0){
			sliderMurderUniv.setEnabled(false);
		}
		
		Set<String> colRobberySet = CrimeLabeledArticles.getArticlesByLabel(UNIV_ROBBERY_CLASS);
		if(colRobberySet==null || colRobberySet.size()==0){
			sliderRoberyUniv.setEnabled(false);
		}

		Set<String> colSexAsltSet = CrimeLabeledArticles.getArticlesByLabel(UNIV_SEXUAL_CRIME_CLASS);
		if(colSexAsltSet==null || colSexAsltSet.size()==0){
			sliderSexAsltUniv.setEnabled(false);
		}

		Set<String> colVlntCrimSet = CrimeLabeledArticles.getArticlesByLabel(UNIV_VLNT_CRIME_CLASS);
		if(colVlntCrimSet==null || colVlntCrimSet.size()==0){
			sliderVioCrmUniv.setEnabled(false);
		}

	}
	
	
	
	
	/**
	 * Create the application.
	 */
	public ApplicationWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmFacetedInterface = new JFrame();
		frmFacetedInterface.setMinimumSize(new Dimension(1200, 750));
		frmFacetedInterface.setTitle("Faceted Interface");
		frmFacetedInterface.setBounds(100, 100, 734, 523);
		frmFacetedInterface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frmFacetedInterface.getContentPane().setLayout(new BorderLayout(0, 0));
		
		JLayeredPane layeredPane = new JLayeredPane();
		layeredPane.setBorder(new LineBorder(new Color(0, 0, 0)));
		frmFacetedInterface.getContentPane().add(layeredPane, BorderLayout.CENTER);
		
		JPanel facetspanel = new JPanel();
		facetspanel.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		facetspanel.setForeground(UIManager.getColor("Button.select"));
		facetspanel.setBounds(12, 12, 197, 699);
		layeredPane.add(facetspanel);
		SpringLayout sl_facetspanel = new SpringLayout();
		facetspanel.setLayout(sl_facetspanel);
		
		JPanel stateFacetPanel = new JPanel();
		sl_facetspanel.putConstraint(SpringLayout.NORTH, stateFacetPanel, 24, SpringLayout.NORTH, facetspanel);
		sl_facetspanel.putConstraint(SpringLayout.WEST, stateFacetPanel, 16, SpringLayout.WEST, facetspanel);
		sl_facetspanel.putConstraint(SpringLayout.EAST, stateFacetPanel, -8, SpringLayout.EAST, facetspanel);
		stateFacetPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		stateFacetPanel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("53px"),
				FormFactory.LABEL_COMPONENT_GAP_COLSPEC,
				ColumnSpec.decode("89px"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("15px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.PARAGRAPH_GAP_ROWSPEC,
				RowSpec.decode("19px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("20px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("15px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("23px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("15px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("23px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("15px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("23px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("15px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("16px"),
				FormFactory.PARAGRAPH_GAP_ROWSPEC,
				RowSpec.decode("15px"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(22dlu;default)"),}));
		
		JLabel lblState = new JLabel("State");
		lblState.setFont(new Font("Dialog", Font.BOLD, 14));
		stateFacetPanel.add(lblState, "2, 2, 3, 1, left, top");
		
		JLabel lblMurder = new JLabel("Murder");
		lblMurder.setLocation(228, 31);
		stateFacetPanel.add(lblMurder, "2, 4, 3, 1, left, fill");
		sliderMurder.setValue(0);
		
		
		sliderMurder.setMaximumSize(new Dimension(145, 23));
		stateFacetPanel.add(sliderMurder, "2, 6, 3, 1, fill, fill");
		sliderMurder.setPreferredSize(new Dimension(155, 16));
		
		JLabel lblRobbery = new JLabel("Robbery");
		lblRobbery.setLocation(228, 89);
		stateFacetPanel.add(lblRobbery, "2, 8, 3, 1, left, top");
		sliderRobbery.setValue(0);
		
		
		sliderRobbery.setMaximumSize(new Dimension(145, 23));
		sliderRobbery.setPreferredSize(new Dimension(150, 15));
		stateFacetPanel.add(sliderRobbery, "2, 10, 3, 1, fill, fill");
		
		JLabel lblAssault = new JLabel("Assault");
		lblAssault.setLocation(228, 141);
		stateFacetPanel.add(lblAssault, "2, 12, 3, 1, left, top");
		sliderAssault.setValue(0);
		
		
		sliderAssault.setMaximumSize(new Dimension(145, 23));
		sliderAssault.setPreferredSize(new Dimension(150, 15));
		stateFacetPanel.add(sliderAssault, "2, 14, 3, 1, fill, fill");
		
		JLabel lblSexualAssault = new JLabel("Sexual Assault");
		lblSexualAssault.setLocation(228, 193);
		stateFacetPanel.add(lblSexualAssault, "2, 16, 3, 1, left, top");
		sliderSexAslt.setValue(0);
		
		
		stateFacetPanel.add(sliderSexAslt, "2, 18, 3, 1, fill, fill");
		
		JLabel lblFirearmPosession = new JLabel("Firearm posession");
		lblFirearmPosession.setLocation(228, 247);
		stateFacetPanel.add(lblFirearmPosession, "2, 20, 3, 1, left, top");
		facetspanel.add(stateFacetPanel);
		sliderFireArm.setValue(0);
		
		
		stateFacetPanel.add(sliderFireArm, "2, 22, 3, 1, fill, top");
		
		JLabel lblVoilentCrime = new JLabel("Violent Crime");
		lblVoilentCrime.setLocation(228, 299);
		stateFacetPanel.add(lblVoilentCrime, "2, 24, 3, 1, left, top");
		sliderVlntCrime.setValue(0);
		
		
		stateFacetPanel.add(sliderVlntCrime, "2, 26, 3, 1");
		
		JPanel panel = new JPanel();
		sl_facetspanel.putConstraint(SpringLayout.SOUTH, stateFacetPanel, -18, SpringLayout.NORTH, panel);
		sl_facetspanel.putConstraint(SpringLayout.NORTH, panel, 342, SpringLayout.NORTH, facetspanel);
		sl_facetspanel.putConstraint(SpringLayout.WEST, panel, 15, SpringLayout.WEST, facetspanel);
		sl_facetspanel.putConstraint(SpringLayout.EAST, panel, -9, SpringLayout.EAST, facetspanel);
		panel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		facetspanel.add(panel);
		panel.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblNewLabel = new JLabel("University");
		panel.add(lblNewLabel, "2, 2");
		
		JLabel lblMurderUniv = new JLabel("Murder");
		panel.add(lblMurderUniv, "2, 4");
		panel.add(sliderMurderUniv, "2, 6");
		sliderMurderUniv.setValue(0);
		
		JLabel lblRobberyUniv = new JLabel("Robbery");
		panel.add(lblRobberyUniv, "2, 8");
		panel.add(sliderRoberyUniv, "2, 10");
		sliderRoberyUniv.setValue(0);
		
		JLabel labelSexAsltUniv = new JLabel("Sexual Assault");
		panel.add(labelSexAsltUniv, "2, 12");
		panel.add(sliderSexAsltUniv, "2, 14");
		sliderSexAsltUniv.setValue(0);
		
		JLabel lblVoilentCrimeUniv = new JLabel("Violent Crime");
		panel.add(lblVoilentCrimeUniv, "2, 16");
		panel.add(sliderVioCrmUniv, "2, 18");
		sliderVioCrmUniv.setValue(0);
		
		JPanel panel_1 = new JPanel();
		sl_facetspanel.putConstraint(SpringLayout.NORTH, panel_1, 577, SpringLayout.NORTH, facetspanel);
		sl_facetspanel.putConstraint(SpringLayout.SOUTH, panel, -7, SpringLayout.NORTH, panel_1);
		panel_1.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
		sl_facetspanel.putConstraint(SpringLayout.WEST, panel_1, 17, SpringLayout.WEST, facetspanel);
		sl_facetspanel.putConstraint(SpringLayout.SOUTH, panel_1, -10, SpringLayout.SOUTH, facetspanel);
		sl_facetspanel.putConstraint(SpringLayout.EAST, panel_1, -8, SpringLayout.EAST, facetspanel);
		facetspanel.add(panel_1);
		panel_1.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblHateCrime = new JLabel("Hate crime");
		panel_1.add(lblHateCrime, "2, 2");
		
		
		panel_1.add(chckbxSexual, "2, 4");
		
		
		panel_1.add(chckbxRace, "2, 6");
		
		JPanel mainPanel = new JPanel();
		mainPanel.setAutoscrolls(true);
		mainPanel.setBorder(new LineBorder(new Color(0, 0, 0), 1, true));
		mainPanel.setBounds(228, 12, 956, 699);
		layeredPane.add(mainPanel);
		SpringLayout sl_mainPanel = new SpringLayout();
		mainPanel.setLayout(sl_mainPanel);
		
		
		editPaneArticles.setEditable(false);
		editPaneArticles.setContentType("text/html");
		JScrollPane scrollPane = new JScrollPane(editPaneArticles);
		editPaneArticles.addHyperlinkListener(new ActionHyperLinkListener());
		
		//mainPanel.add(scrollPane,BorderLayout.CENTER);
		
		
		//dtrpnArticles.setText("Articles");
		
		sl_mainPanel.putConstraint(SpringLayout.NORTH, scrollPane, 9, SpringLayout.NORTH, mainPanel);
		sl_mainPanel.putConstraint(SpringLayout.WEST, scrollPane, 9, SpringLayout.WEST, mainPanel);
		sl_mainPanel.putConstraint(SpringLayout.SOUTH, scrollPane, -1, SpringLayout.SOUTH, mainPanel);
		sl_mainPanel.putConstraint(SpringLayout.EAST, scrollPane, -1, SpringLayout.EAST, mainPanel);
		mainPanel.add(scrollPane);
		//mainPanel.add(editPaneArticles);

		
		sliderMurder.addChangeListener(
				new ChangeListener() {
					@Override
					public void stateChanged(ChangeEvent e) {
						JSlider slider=(JSlider)e.getSource();
						if(!slider.getValueIsAdjusting()){
							int murderValue =sliderMurder.getValue();
							Set<String> statesWithinMurderRange =StateMurderRangeValues.getRangeValues(murderValue);
							intersectStateCrimeArticles(STATE_MURDER_CLASS, murderValue,statesWithinMurderRange);
							Set<String> articlesChoosen=doMultiFacetArticleIntersection();
							Set<String> stateRange =doStateInersection();
							composeHtmlTextAndDisplay(stateRange,articlesChoosen);
						}
					}
				}

		);
		
		sliderRobbery.addChangeListener(
				new ChangeListener() {
					@Override
					public void stateChanged(ChangeEvent e) {
						JSlider slider=(JSlider)e.getSource();
						if(!slider.getValueIsAdjusting()){
							int robberyValue =sliderRobbery.getValue();
							Set<String> statesWithinRobberRange =StateRobberyRangeValues.getRangeValues(robberyValue);
							intersectStateCrimeArticles(STATE_ROBBERY_CLASS, robberyValue,statesWithinRobberRange);
							Set<String> articlesChoosen=doMultiFacetArticleIntersection();
							Set<String> stateRange =doStateInersection();
							composeHtmlTextAndDisplay(stateRange,articlesChoosen);
						}
					}
				}

		);

		sliderAssault.addChangeListener(
				new ChangeListener() {
					@Override
					public void stateChanged(ChangeEvent e) {
						JSlider slider=(JSlider)e.getSource();
						if(!slider.getValueIsAdjusting()){
							int asltValue =sliderAssault.getValue();
							Set<String> statesWithinAsltRange =StateAggrAssaultRangeValues.getRangeValues(asltValue);
							intersectStateCrimeArticles(STATE_ASSAULT_CLASS, asltValue,statesWithinAsltRange);
							Set<String> articlesChoosen=doMultiFacetArticleIntersection();
							Set<String> stateRange =doStateInersection();
							composeHtmlTextAndDisplay(stateRange,articlesChoosen);
						}
					}
				}

		);
		
		sliderSexAslt.addChangeListener(				
				new ChangeListener() {
					@Override
					public void stateChanged(ChangeEvent e) {
						JSlider slider=(JSlider)e.getSource();
						if(!slider.getValueIsAdjusting()){
							int sexAsltValue =sliderSexAslt.getValue();
							Set<String> statesWithinSexAsltRange =StateSexAsltRangeValues.getRangeValues(sexAsltValue);
							intersectStateCrimeArticles(STATE_SEXUAL_ASLT_CLASS, sexAsltValue,statesWithinSexAsltRange);
							Set<String> articlesChoosen=doMultiFacetArticleIntersection();
							Set<String> stateRange =doStateInersection();
							composeHtmlTextAndDisplay(stateRange,articlesChoosen);
						}
					}
				}
		);

		sliderFireArm.addChangeListener(				
				new ChangeListener() {
					@Override
					public void stateChanged(ChangeEvent e) {
						JSlider slider=(JSlider)e.getSource();
						if(!slider.getValueIsAdjusting()){
							int fireArmValue =sliderFireArm.getValue();
							Set<String> statesWithinFireArmRange =StateFirearmRangeValues.getRangeValues(fireArmValue);
							intersectStateCrimeArticles(STATE_FIRE_ARM_CLASS, fireArmValue,statesWithinFireArmRange);
							Set<String> articlesChoosen=doMultiFacetArticleIntersection();
							Set<String> stateRange =doStateInersection();
							composeHtmlTextAndDisplay(stateRange,articlesChoosen);
						}
					}
				}
		);

		sliderVlntCrime.addChangeListener(
				new ChangeListener() {					
					@Override
					public void stateChanged(ChangeEvent e) {
						JSlider slider=(JSlider)e.getSource();
						if(!slider.getValueIsAdjusting()){
							int vlntCrimeValue =sliderVlntCrime.getValue();
							Set<String> statesWithinvlntCrimRange =StateFirearmRangeValues.getRangeValues(vlntCrimeValue);
							intersectStateCrimeArticles(STATE_VLNT_CRIME_CLASS, vlntCrimeValue,statesWithinvlntCrimRange);
							Set<String> articlesChoosen=doMultiFacetArticleIntersection();
							Set<String> stateRange =doStateInersection();
							composeHtmlTextAndDisplay(stateRange,articlesChoosen);
						}
					}
				}

		);

	
		sliderMurderUniv.addChangeListener(
				new ChangeListener() {
					@Override
					public void stateChanged(ChangeEvent e) {
						JSlider slider=(JSlider)e.getSource();
						if(!slider.getValueIsAdjusting()){
							int murderValue =sliderMurderUniv.getValue();
							Set<String> univWithinMurderRange =UniversityMurderRangeValues.getRangeValues(murderValue);
							//System.out.println(univWithinMurderRange);
							intersectUniversityCrimeArticles(UNIV_MURDER_CLASS, murderValue,univWithinMurderRange);
							Set<String> articlesChoosen=doMultiFacetArticleIntersection();
							Set<String> universityRange =doUniversityInersection();
							composeHtmlTextAndDisplay(universityRange,articlesChoosen);
						}
					}
				}
		);
		
		sliderRoberyUniv.addChangeListener(
				new ChangeListener() {
					@Override
					public void stateChanged(ChangeEvent e) {
						JSlider slider=(JSlider)e.getSource();
						if(!slider.getValueIsAdjusting()){
							int robberyValue =sliderRoberyUniv.getValue();
							Set<String> univWithinRobberRange =UniversityRobberyRangeValues.getRangeValues(robberyValue);
							//System.out.println(univWithinRobberRange);
							intersectUniversityCrimeArticles(UNIV_ROBBERY_CLASS, robberyValue,univWithinRobberRange);
							Set<String> articlesChoosen=doMultiFacetArticleIntersection();
							Set<String> universityRange =doUniversityInersection();
							composeHtmlTextAndDisplay(universityRange,articlesChoosen);
						}
					}
				}
		);
		
		sliderSexAsltUniv.addChangeListener(
				new ChangeListener() {
					@Override
					public void stateChanged(ChangeEvent e) {
						JSlider slider=(JSlider)e.getSource();
						if(!slider.getValueIsAdjusting()){
							int sexAsltValue =sliderSexAsltUniv.getValue();
							Set<String> univWithinSexAsltRange =UniversitySexAsltRangeValues.getRangeValues(sexAsltValue);
							//System.out.println(univWithinSexAsltRange);
							intersectUniversityCrimeArticles(UNIV_SEXUAL_CRIME_CLASS, sexAsltValue,univWithinSexAsltRange);
							Set<String> articlesChoosen=doMultiFacetArticleIntersection();
							Set<String> universityRange =doUniversityInersection();
							composeHtmlTextAndDisplay(universityRange,articlesChoosen);
						}
					}
				}
		);

		sliderVioCrmUniv.addChangeListener(				
				new ChangeListener() {
					@Override
					public void stateChanged(ChangeEvent e) {
						JSlider slider=(JSlider)e.getSource();
						if(!slider.getValueIsAdjusting()){
							int vlntCrimeValue = sliderVioCrmUniv.getValue();
							Set<String> univWithinvlntCrimRange =UniversityVlntCrimeRangeValues.getRangeValues(vlntCrimeValue);
							//System.out.println(univWithinvlntCrimRange);
							intersectUniversityCrimeArticles(UNIV_VLNT_CRIME_CLASS, vlntCrimeValue,univWithinvlntCrimRange);
							Set<String> articlesChoosen=doMultiFacetArticleIntersection();
							Set<String> universityRange =doUniversityInersection();
							composeHtmlTextAndDisplay(universityRange,articlesChoosen);
						}
					}
				}
		);
		
		enableFacets();
	}

	public Set<String> doStateInersection(){
		Set<String> intersectedRangeValues = new HashSet<String>();
		ArrayList<Set<String>> listOfIntersectedSets = new ArrayList<Set<String>>();
		
		int murderValue =sliderMurder.getValue();
		if(murderValue>0){
			Set<String> statesWithinMurderRange =StateMurderRangeValues.getRangeValues(murderValue);
			if(statesWithinMurderRange!=null && statesWithinMurderRange.size()>0){
				//System.out.println("murder: "+statesWithinMurderRange);
				listOfIntersectedSets.add(statesWithinMurderRange);
			}
		}

		int robberyValue =sliderRobbery.getValue();
		if(robberyValue>0){
			Set<String> statesWithinRobberRange =StateRobberyRangeValues.getRangeValues(robberyValue);
			if(statesWithinRobberRange!=null && statesWithinRobberRange.size()>0){
				//System.out.println("robbery: "+statesWithinRobberRange);
				listOfIntersectedSets.add(statesWithinRobberRange);
			}
		}
		int assltCrimeValue =sliderAssault.getValue();
		if(assltCrimeValue>0){
			Set<String> statesWithinvlntCrimRange =StateAggrAssaultRangeValues.getRangeValues(assltCrimeValue);
			if(statesWithinvlntCrimRange!=null && statesWithinvlntCrimRange.size()>0){
				//System.out.println("vlntcrime: "+statesWithinvlntCrimRange);
				listOfIntersectedSets.add(statesWithinvlntCrimRange);
			}
		}
		
		int sexAsltValue =sliderSexAslt.getValue();
		if(sexAsltValue>0){
			Set<String> statesWithinSexAsltRange =StateSexAsltRangeValues.getRangeValues(sexAsltValue);
			if(statesWithinSexAsltRange!=null && statesWithinSexAsltRange.size()>0){
				//System.out.println("sexaslt: "+statesWithinSexAsltRange);
				listOfIntersectedSets.add(statesWithinSexAsltRange);
			}
		}
	
		int fireArmValue =sliderFireArm.getValue();
		if(fireArmValue>0){
			Set<String> statesWithinFireArmRange =StateFirearmRangeValues.getRangeValues(fireArmValue);
			if(statesWithinFireArmRange!=null && statesWithinFireArmRange.size()>0){
				//System.out.println("firearm: "+statesWithinFireArmRange);
				listOfIntersectedSets.add(statesWithinFireArmRange);
			}
		}
		
		int vlntCrimeValue =sliderVlntCrime.getValue();
		if(vlntCrimeValue>0){
			Set<String> statesWithinvlntCrimRange =StateFirearmRangeValues.getRangeValues(vlntCrimeValue);
			if(statesWithinvlntCrimRange!=null && statesWithinvlntCrimRange.size()>0){
				//System.out.println("vlntcrime: "+statesWithinvlntCrimRange);
				listOfIntersectedSets.add(statesWithinvlntCrimRange);
			}
		}
		
		if(listOfIntersectedSets.size()>0){
			intersectedRangeValues.addAll(listOfIntersectedSets.get(0));
			//System.out.println("listOfIntersectedSets.get(0)"+ listOfIntersectedSets.get(0));
			for(Set<String> setRange : listOfIntersectedSets){
				//System.out.println("listOfIntersectedSets "+ setRange);
				intersectedRangeValues.retainAll(setRange);
			}
		}
		System.out.println(intersectedRangeValues);
		//editPaneArticles.setText(intersectedRangeValues.toString());
		return intersectedRangeValues;
	}

	
	
	
	public Set<String> doUniversityInersection(){
		Set<String> intersectedRangeValues = new HashSet<String>();
		ArrayList<Set<String>> listOfIntersectedSets = new ArrayList<Set<String>>();
		
		int murderValue =sliderMurderUniv.getValue();
		if(murderValue>0){
			Set<String> univWithinMurderRange =UniversityMurderRangeValues.getRangeValues(murderValue);
			if(univWithinMurderRange!=null && univWithinMurderRange.size()>0){
				//System.out.println("univWithinMurderRange "+univWithinMurderRange );
				listOfIntersectedSets.add(univWithinMurderRange);
			}
		}

		int robberyValue =sliderRoberyUniv.getValue();
		if(robberyValue>0){
			Set<String> univWithinRobberRange =UniversityRobberyRangeValues.getRangeValues(robberyValue);
			if(univWithinRobberRange!=null && univWithinRobberRange.size()>0){
				//System.out.println("univWithinRobberRange "+univWithinRobberRange );
				listOfIntersectedSets.add(univWithinRobberRange);
			}
		}
		
		int sexAsltValue =sliderSexAsltUniv.getValue();
		if(sexAsltValue>0){
			Set<String> univWithinSexAsltRange =UniversitySexAsltRangeValues.getRangeValues(sexAsltValue);
			if(univWithinSexAsltRange!=null && univWithinSexAsltRange.size()>0){
				//System.out.println("univWithinSexAsltRange "+univWithinSexAsltRange);
				listOfIntersectedSets.add(univWithinSexAsltRange);
			}
		}
	
		int vlntCrimeValue =sliderVioCrmUniv.getValue();
		if(vlntCrimeValue>0){
			Set<String> univWithinvlntCrimRange =UniversityVlntCrimeRangeValues.getRangeValues(vlntCrimeValue);
			if(univWithinvlntCrimRange!=null && univWithinvlntCrimRange.size()>0){
				//System.out.println("univWithinvlntCrimRange "+univWithinvlntCrimRange);
				listOfIntersectedSets.add(univWithinvlntCrimRange);
			}
		}
		if(listOfIntersectedSets.size()>0){
			intersectedRangeValues.addAll(listOfIntersectedSets.get(0)); 
			for(Set<String> setRange : listOfIntersectedSets){
				//System.out.println(setRange);
				intersectedRangeValues.retainAll(setRange);
			}
		}
		System.out.println(intersectedRangeValues);
		//editPaneArticles.setText(intersectedRangeValues.toString());
		return intersectedRangeValues;
	}


	public Set<String> intersectStateCrimeArticles(String label,int value,Set<String> statesWithinRange){
		Set<String> crimeArticleSet = new HashSet<String>();		

		if(value>0){
			Set<String> crimeSet=CrimeLabeledArticles.getArticlesByLabel(label);
			if(crimeSet!=null){
				crimeArticleSet.addAll(crimeSet);
			}
			System.out.println("crimeArticleSet :"+crimeArticleSet);
			
			Set<String> statesArticleSet = new HashSet<String>();
			if(statesWithinRange!=null && statesWithinRange.size()>0){
				Set<String> rowSet =StateLabeledArticles.getArticlesByStates(statesWithinRange);
				if(rowSet!=null){
					statesArticleSet.addAll(rowSet);
				}
			}	
			System.out.println("statesArticleSet :"+statesArticleSet);
			
			if(crimeArticleSet!=null && crimeArticleSet.size()>0){
				if(statesArticleSet!=null && statesArticleSet.size()>0){
					crimeArticleSet.retainAll(statesArticleSet);
				}
			}
		}
		
		facetArtileMap.put(label, crimeArticleSet);
		System.out.println("Intersected articles :"+crimeArticleSet);
		return crimeArticleSet;
	}
	
	
	public Set<String> intersectUniversityCrimeArticles(String label, int value,Set<String> univWithinRange ){

		Set<String> crimeArticleSet = new HashSet<String>();
		if(value>0){
			Set<String> crimeSet=CrimeLabeledArticles.getArticlesByLabel(label);
			if(crimeSet!=null){
				crimeArticleSet.addAll(crimeSet);
			}
			System.out.println("crimeArticleSet :"+crimeArticleSet);
			
			Set<String> universityArticleSet = new HashSet<String>();
			if(univWithinRange!=null && univWithinRange.size()>0){
				Set<String> rowSet =UniversityLabeledArticles.getArticlesByUniversities(univWithinRange);
				if(rowSet!=null){
					universityArticleSet.addAll(rowSet);
				}
			}
			System.out.println("universityArticleSet :"+universityArticleSet);
			
			if(crimeArticleSet!=null && crimeArticleSet.size()>0){
				if(universityArticleSet!=null && universityArticleSet.size()>0){
					crimeArticleSet.retainAll(universityArticleSet);
				}
			}
		}
		facetArtileMap.put(label, crimeArticleSet);
		System.out.println("Intersected articles :"+crimeArticleSet);
		return crimeArticleSet;
	}

	
	public Set<String> doMultiFacetArticleIntersection(){
		fetchHateCrimeArticles();
		Set<String> intersectedArtilces = new HashSet<String>();
		boolean firstTime=true;
		if(facetArtileMap.size()>0){
			Collection<Set<String>> facetArticleCollection =facetArtileMap.values();
			if(facetArticleCollection!=null && facetArticleCollection.size()>0){
				for(Set<String> facetArticle :facetArticleCollection){
					//System.out.println("facetArticle :"+facetArticle);
					if(facetArticle!=null && facetArticle.size()>0){
						if(firstTime){
							intersectedArtilces.addAll(facetArticle);
							firstTime=false;
						}
						else{
							intersectedArtilces.retainAll(facetArticle);
						}
					}
				}
			}
		}
		return intersectedArtilces;
	}
	
	public void fetchHateCrimeArticles(){

		Set<String> antiSexualArticles = new HashSet<String>();
		if(chckbxSexual.isSelected()){
			Set<String> articlesSet = HateCrimeLabeledArticles.getArticlesBySexual();
			if(articlesSet!=null && articlesSet.size()>0){
				antiSexualArticles.addAll(articlesSet);
			}	
		}
		System.out.println("anti sexual "+antiSexualArticles);
		Set<String> antiRaceArticles = new HashSet<String>();
		if(chckbxRace.isSelected()){
			Set<String> articlesSet = HateCrimeLabeledArticles.getArticlesByRace();
			if(articlesSet!=null && articlesSet.size()>0){
				antiRaceArticles.addAll(articlesSet);
			}	
		}
		System.out.println("anti race "+antiRaceArticles);
		if(antiSexualArticles!=null && antiSexualArticles.size()>0){
			facetArtileMap.put("sexual", antiSexualArticles);
		}
		if(antiRaceArticles!=null && antiRaceArticles.size()>0){
			facetArtileMap.put("race", antiRaceArticles);
		}	

	}

	
	public void composeHtmlTextAndDisplay(Set<String> rowLabels,Set<String> articleSet){
		StringBuffer linkRows = new StringBuffer();
		System.out.println("final articles :"+articleSet);
		if(articleSet!=null && articleSet.size()>0){
		
			for(String link : articleSet){
				linkRows.append("<br><p><a href="+link+">"+link+"</a></p><br>") ;
			}
			String articleTable ="<table padding-right:2px;>" +
			"<th  style=\"padding-bottom:2px;padding-left:5px;padding-top:2px; \">"+articleSet.size()+" Articles Selected </th>"+
				"<tr>"+
					"<p>"+rowLabels+"</p>"+
				"</tr>"+
				"<tr>" +
					"<div style=\"border-width:1px;border-style:outset;\" > " +
					"<p> Artice Title <p>"+linkRows.toString()+ 
					"</div> " +
				"</tr>"+
	  		"</table>";
			editPaneArticles.setText(articleTable);
		}
		else 
			editPaneArticles.setText("<p>"+rowLabels+"</p>");
	}
}
